<?php
	require_once("../conexion/conexion.php");
	
	function BuscaRecurso($numero)
	{
		$con = new Conexion();
		$con2 = $con->conexionBD("fpproduccion");
		$sql="select ReNombre from recurso inner join recursodia on recursodia.ReCod=recurso.ReCod where DiaCod='".$numero."' order by ReNombre asc";
		$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
		$fila = mysql_num_rows($res);
		$con->cerrarBD();
		$cadena = "";
		$aux=0;
		while($reg = mysql_fetch_array($res))
		{
			if($aux==0)
			{
				$aux=1;
				$cadena = $reg['ReNombre'];
			}
			else
			{
				$cadena = $cadena."<br>".$reg['ReNombre'];
			}
		}
		
		return $cadena;
	}
	
	function BuscaCategoria($numero)
	{
		$con = new Conexion();
		$con2 = $con->conexionBD("fpproduccion");
		$sql="select CatTipo from categoria inner join catdia on catdia.CatCod=categoria.CatCod where DiaCod='".$numero."' order by CatTipo asc";
		$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
		$fila = mysql_num_rows($res);
		$con->cerrarBD();
		$cadena = "";
		$aux=0;
		while($reg = mysql_fetch_array($res))
		{
			if($aux==0)
			{
				$aux=1;
				$cadena = $reg['CatTipo'];
			}
			else
			{
				$cadena = $cadena."<br>".$reg['CatTipo'];
			}
		}
		
		return $cadena;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<link href="../Estilos/global.css" rel="stylesheet" type="text/css" />
<link href="../Estilos/local.css" rel="stylesheet" type="text/css" />
<link href="../Estilos/CssTabla3.css" rel="stylesheet" type="text/css" />
<script src="../include_js/jquery.js" type="text/javascript"></script>
<style>
.filaPar
{
	background:#81D371; 
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.filaImpar
{
	background:#AEF1A1;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
a:link {text-decoration:line-through; color:#0033FF;} /* Link no visitado*/
a:visited {text-decoration:none; color:#999900} /*Link visitado*/
a:hover {text-decoration:underline; color:#990000;} /*Mouse sobre el link*/
</style>
<script>
function Formulario(valor)
{
	
	id = document.getElementById("tbDia").rows[valor].cells[0].innerText;
	numero = document.getElementById("tbDia").rows[valor].cells[1].innerText;
	//alert(id+" "+numero);
	window.open("Produccion/Ventana_Dia.php?id="+id+"&num="+numero,"VentanaResumenDia","width=590,height=530,scrollbars=yes");
}
</script>
</head>

<body>
 <?php
 	extract($_POST);
	$cont = 0;
	
 	if ($txtTitulo1=="")
	{
		$txtTitulo1="null";
		$cont++;
	}
	else
	{$txtTitulo1="'".$txtTitulo1."'";}
	
	if ($txtSolicitud=="") {
		$txtSolicitud="null";
		$cont++;
	}
	else
	{$txtSolicitud="'".$txtSolicitud."'";}
	
	if ($cmb_Categoria1=='0') {
		$cmb_Categoria1="null";
		$cont++;
	}
	//fecha inicio
	if ($txtFIni==""){
		$txtFIni="null";
		$cont++;
	}
	else
	{
		$FechaIni = explode("-", $txtFIni);
		$txtFIni = "$FechaIni[2]-$FechaIni[1]-$FechaIni[0]";
	}
	//fecha fin
	if ($txtFFin==""){
		$txtFFin="null";
		$cont++;
	}
	else
	{
		$FechaFin = explode("-", $txtFFin);
		$txtFFin = "$FechaFin[2]-$FechaFin[1]-$FechaFin[0]";
	}
	
 	if($cont != 5)
	{
		$con = new Conexion();
		$con2 = $con->conexionBD("fpproduccion");
		$sql = "select distinct
				dia.DiaCod,
				DiaSolicitud,
				CliRSocial,
				date_format(DiaFechaIni,'%d/%m/%Y') as fecha,
				DiaValor
				
				from dia
				inner join catdia on
				catdia.DiaCod=dia.DiaCod
				inner join categoria on
				categoria.CatCod=catdia.CatCod
				inner join recursodia on
				recursodia.DiaCod=dia.DiaCod
				inner join recurso on
				recurso.ReCod=recursodia.ReCod
				inner join clientes on
				clientes.CliRut=dia.CliRut
				
				where
				(($txtTitulo1 is NULL) or (DiaTitulo = $txtTitulo1)) and
				(($cmb_Categoria1 is NULL) or (catdia.CatCod = $cmb_Categoria1)) and
				(($txtSolicitud is NULL) or (DiaSolicitud = $txtSolicitud)) and
				((($txtFIni is NULL) and ($txtFFin is NULL)) or DATE(DiaFechaIni) BETWEEN '".$txtFIni."' and '".$txtFFin."')
				order by dia.DiaCod asc";
				//echo $sql;
			$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
			$fila = mysql_num_rows($res);
			$con->cerrarBD();
			if($fila >0)
			{?>
				<table width="100%" border="0" class="uno" cellpadding="0" cellspacing="0" id="tbDia">
				<thead>
  				<tr>
					<th width="3%">id</th>
    				<th width="12%">N&deg; Solicitud</th>
				    <th width="32%">Titular</th>
				    <th width="20%">Recurso</th>
				    <th width="13%">Fecha</th>
				    <th width="11%">Categoria</th>
					<th width="9%">valor (uf)</th>
  				</tr>
 				</thead>
				<?php
				$aux=1;
				$cont=1;
				while($reg = mysql_fetch_array($res))
				{
					$recursos = BuscaRecurso($reg['DiaCod']);
					$categorias = BuscaCategoria($reg['DiaCod']);
					if(($cont % 2)==0)
					{
						echo "<tr class='FilaPar' id='".$aux."'>";
						echo "<td width=\"3%\" class='FilaPar'>".$reg['DiaCod']."</td>";
						echo "<td width=\"12%\" class='FilaPar' id='".$aux."'><a href=javascript:Formulario($aux);>".$reg['DiaSolicitud']."</a></td>";
						echo "<td width=\"32%\" class='FilaPar'>".$reg['CliRSocial']."</td>";
						echo "<td width=\"20%\" class='FilaPar'>".$recursos."</td>";
						echo "<td width=\"13%\" class='FilaPar'>".$reg['fecha']."</td>";
						echo "<td width=\"11%\" class='FilaPar'>".$categorias."</td>";
						echo "<td width=\"9%\" class='FilaPar'>".$reg['DiaValor']."</td>";
						echo "</tr>";
					}
					else
					{
						echo "<tr class='FilaImpar' id='".$aux."'>";
						echo "<td width=\"3%\" class='FilaImpar'>".$reg['DiaCod']."</td>";
						echo "<td width=\"12%\" class='FilaImpar'><a href=javascript:Formulario($aux);>".$reg['DiaSolicitud']."</a></td>";
						echo "<td width=\"32%\" class='FilaImpar'>".$reg['CliRSocial']."</td>";
						echo "<td width=\"20%\" class='FilaImpar'>".$recursos."</td>";
						echo "<td width=\"13%\" class='FilaImpar'>".$reg['fecha']."</td>";
						echo "<td width=\"11%\" class='FilaImpar'>".$categorias."</td>";
						echo "<td width=\"9%\" class='FilaImpar'>".$reg['DiaValor']."</td>";
						echo "</tr>";
					}
					$cont++;
					$aux++;
				}
				
				?></table><?php
			}
			else
				{echo "<label style=\"font:Verdana, Arial, Helvetica, sans-serif; color:#0066CC; font-weight:bold;\">La b&uacute;squeda no gener&oacute; resultados.</label>";}
	}
?>
</body>
</html>
